﻿<!DOCTYPE html>
<html>
<head>
    <title>JS Arrays Exercise 3</title>
    <link href="styles/js-console.css" rel="stylesheet" />
</head>
<body>
    <h1>Finds the maximal sequence of equal elements in an array.</h1>
    <label for="arr-input">Array:</label>
    <input type="text" id="arr-input" />
    <button onclick="execute()">Execute</button>
    <div id="js-console"></div>
    <script src="scripts/js-console.js"></script>
    <script>
        function execute()
        {
            var arr_string = jsConsole.read("#arr-input");
            var val = arr_string.split(", ");
            var maxCount = 0;
            var maxStartPos = 0;
            var currCount = 0;
            var lastVal;

            if (val.length > 0)
            {
                // Get first element value
                lastVal = val[0];
                currCount = 1;
                maxCount = 1;
                maxStartPos = 0;

                // Cycle all elements after the first
                for (var index = 1; index < val.length; index++)
                {
                    if (val[index] == lastVal)
                    {
                        // If value is equal to last value then increase the counter
                        currCount++;
                        if (currCount > maxCount)
                        {
                            // If current counter if greater than last found max counter then set max counter to current counter
                            maxCount = currCount;
                            maxStartPos = index - currCount + 1;
                        }
                    }
                    else
                    {
                        // If values are not equal set new lastVal
                        lastVal = val[index];
                        currCount = 1;
                    }
                }

                jsConsole.writeLine(val.splice(maxStartPos, maxCount));
            }
        }
    </script>
</body>
</html>
